﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace TPCN.DAL
{
   public class ConfigDAL
    {
        public static ConfigObj GetByID(string ConfigID)
        {
            ConfigObj obj = new ConfigObj();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "sp_tbConfig_GetByID";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@ConfigID", ConfigID);
                using (SqlDataReader dr = Database.db.GetDataToDataReader(cmd))
                {
                    if (dr.HasRows)
                    {
                        dr.Read();
                        obj = obj.SetValuesFromDataReader(dr);
                    }
                    dr.Close();
                }
            }
            return obj;
        }

        public static List<ConfigObj> GetByAll()
        {
            List<ConfigObj> result = new List<ConfigObj>();
            ConfigObj obj = new ConfigObj();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "sp_tbConfig_GetByAll";
                cmd.CommandType = CommandType.StoredProcedure;
                using (SqlDataReader dr = Database.db.GetDataToDataReader(cmd))
                {
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            result.Add(obj.SetValuesFromDataReader(dr));
                        }
                    }
                    dr.Close();
                    obj = null;
                }
            }
            return result;
        }

        public static bool Insert(ConfigObj obj)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "sp_tbConfig_Insert";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@DefaultTitle", (obj.DefaultTitle == null) ? String.Empty : obj.DefaultTitle);
                    cmd.Parameters.AddWithValue("@DefaultKeyword", (obj.DefaultKeyword == null) ? String.Empty : obj.DefaultKeyword);
                    cmd.Parameters.AddWithValue("@DefaultDescription", (obj.DefaultDescription == null) ? String.Empty : obj.DefaultDescription);
                    cmd.Parameters.AddWithValue("@SaleEmail", (obj.SaleEmail == null) ? String.Empty : obj.SaleEmail);
                    cmd.Parameters.AddWithValue("@TypeMail", (obj.TypeMail == null) ? String.Empty : obj.TypeMail);
                    cmd.Parameters.AddWithValue("@NoreplyMail", (obj.NoreplyMail == null) ? String.Empty : obj.NoreplyMail);
                    cmd.Parameters.AddWithValue("@NoreplyPass", (obj.NoreplyPass == null) ? String.Empty : obj.NoreplyPass);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool Update(ConfigObj obj)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "sp_tbConfig_Update";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@ConfigID", (obj.ConfigID == null) ? String.Empty : obj.ConfigID);
                    cmd.Parameters.AddWithValue("@DefaultTitle", (obj.DefaultTitle == null) ? String.Empty : obj.DefaultTitle);
                    cmd.Parameters.AddWithValue("@DefaultKeyword", (obj.DefaultKeyword == null) ? String.Empty : obj.DefaultKeyword);
                    cmd.Parameters.AddWithValue("@DefaultDescription", (obj.DefaultDescription == null) ? String.Empty : obj.DefaultDescription);
                    cmd.Parameters.AddWithValue("@SaleEmail", (obj.SaleEmail == null) ? String.Empty : obj.SaleEmail);
                    cmd.Parameters.AddWithValue("@TypeMail", (obj.TypeMail == null) ? String.Empty : obj.TypeMail);
                    cmd.Parameters.AddWithValue("@NoreplyMail", (obj.NoreplyMail == null) ? String.Empty : obj.NoreplyMail);
                    cmd.Parameters.AddWithValue("@NoreplyPass", (obj.NoreplyPass == null) ? String.Empty : obj.NoreplyPass);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool Delete(string ConfigID)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "sp_tbConfig_DeleteByID";
                    cmd.Parameters.AddWithValue("@ConfigID", ConfigID);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
